package top.fengleifeng.question;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-10-19 11:31
 **/
public class Num1460通过翻转子数组使两个数组相等 {
    class Solution {
        public boolean canBeEqual(int[] target, int[] arr) {
            Map<Integer, Integer> targetMap = new HashMap<>();
            Map<Integer, Integer> arrMap = new HashMap<>();
            for (int i = 0; i < target.length; i++) {
                if (targetMap.containsKey(target[i])) {
                    targetMap.put(target[i], targetMap.get(target[i]) + 1);
                } else {
                    targetMap.put(target[i], 1);
                }
            }
            for (int i = 0; i < arr.length; i++) {
                if (arrMap.containsKey(arr[i])) {
                    arrMap.put(arr[i], arrMap.get(arr[i]) + 1);
                } else {
                    arrMap.put(arr[i], 1);
                }
            }

            Iterator<Map.Entry<Integer, Integer>> iterator= targetMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer,Integer> entry = iterator.next();
                Integer key = entry.getKey();
                Integer value = entry.getValue()    ;

                Integer integer = arrMap.get(key);
                if(integer==null || value.intValue()!=integer.intValue()){
                    return false;
                }
            }
            return true;
        }
    }
}
